﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;
using GriffinWebService.Entity;
using GriffinWebService.BE;

namespace GriffinWebService.DALC
{
    public class userDALC
    {
        SqlHelper sqlHelper = new SqlHelper();

        #region Create methods
        public User createUser(User entity)
        {

            string statement = string.Empty;

            try
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }
                statement = "dbo.spu_user_create";
                if (string.IsNullOrEmpty(entity.Username))
                {
                    sqlHelper.AddParameterToSQLCommand("@username", SqlDbType.NVarChar, DBNull.Value);
                }
                else
                {
                    sqlHelper.AddParameterToSQLCommand("@username", SqlDbType.NVarChar, entity.Username);
                }
                if (string.IsNullOrEmpty(entity.Password))
                {
                    sqlHelper.AddParameterToSQLCommand("@password", SqlDbType.NVarChar, DBNull.Value);
                }
                else
                {
                    sqlHelper.AddParameterToSQLCommand("@password", SqlDbType.NVarChar, entity.Password);
                }
                if (string.IsNullOrEmpty(entity.Email))
                {
                    sqlHelper.AddParameterToSQLCommand("@email", SqlDbType.NVarChar, DBNull.Value);
                }
                else
                {
                    sqlHelper.AddParameterToSQLCommand("@email", SqlDbType.NVarChar, entity.Email);
                }
                int res = sqlHelper.GetExecuteScalarByCommand(statement);
                if (res > 0)
                {
                    entity.UserID = res;
                }

            }
            finally
            {
                sqlHelper.Dispose();
            }

            return entity;
        }


        #endregion

        #region Find methods
        public User FindByUserNameAndPassword(string username,string password)
        {
            string statement = string.Empty;
            IDataReader dataReader = null;
            User result = null;

            #region
            try
            {
                statement = "dbo.spu_user_find_by_username_password";
                sqlHelper.AddParameterToSQLCommand("@username", SqlDbType.NVarChar, username);
                sqlHelper.AddParameterToSQLCommand("@password", SqlDbType.NVarChar, password);
                dataReader = sqlHelper.GetReaderByCmd(statement);
                if (dataReader.Read())
                {
                    result = new User();
                    int userIDIndex = dataReader.GetOrdinal("UserID");
                    if (!dataReader.IsDBNull(userIDIndex))
                    {
                        result.UserID = dataReader.GetInt32(userIDIndex);
                    }
                    int usernameIndex = dataReader.GetOrdinal("Username");
                    if (!dataReader.IsDBNull(usernameIndex))
                    {
                        result.Username = dataReader.GetString(usernameIndex);
                    }
                    int passwordIndex = dataReader.GetOrdinal("Password");
                    if (!dataReader.IsDBNull(passwordIndex))
                    {
                        result.Password = dataReader.GetString(passwordIndex);
                    }
                    int emailIndex = dataReader.GetOrdinal("Email");
                    if (!dataReader.IsDBNull(emailIndex))
                    {
                        result.Email = dataReader.GetString(emailIndex);
                    }

                }
            }
            finally
            {
                sqlHelper.Dispose();
            }
            #endregion 


            return result;


        }


        #endregion
    }
}
